// pages/desc/desc.js
const app = getApp()
import {
  Index
} from 'index-model.js';
import {
  Config
} from '../../../utils/config.js';
var Function = require("../../../utils/function.js");
var index = new Index(); //实例化 首页 对象c.js
Page({

  data: {
    showpain: false,
    opaintype: '',
    tq: {},
    zfad: false,
    gphoneV: '',
    a: [],
    c: [],
    b: [],
    d: [],
    bd: '',
    wtype: []
  },

  /* 页面跳转，支持tab和普通页面跳转 */
  link: function(e) {
    var url = Function.getDataSet(e, "surl");
    Function.linkTo(url);
  },

  onLoad: function(options) {
    this.setData({
      tq: JSON.parse(options.tq),
      count: options.count
    })
  },
  paintype(e) {
    let query = e.currentTarget.dataset['index'];
    this._getrcoeB(query)
    this.setData({
      opaintype: query,
      showpain: true
    })
  },
  _getrcoe() {
    let that = this;
    let data = {
      id: wx.getStorageSync('members')['id']
    };
    index.getrcoe(data, (res) => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          count: res.count
        })
        if (Array.isArray(res.data)) {
          that.ragarr(res.data)
        } else {

        }

      }
    })
  },

  ragarr(data) {

    var a = data.filter((item, index) => {
      return item.type == 1
    })
    var b = data.filter((item, index) => {
      return item.type == 2
    })
    var c = data.filter((item, index) => {
      return item.type == 3
    })
    var d = data.filter((item, index) => {
      return item.type == 4
    })

    this.setData({
      a,
      b,
      c,
      d
    })

  },

  //领取红包
  hidezz: function() {
    let that = this;
    that.setData({
      zfad: false
    })
  },

  //显示输入手机框
  showModel: function() {
    let that = this;
    that.setData({
      zfad: true
    })
  },
  getPhonev: function(e) {
    let that = this;
    let gphoneV = e.detail.value;
    that.setData({
      gphoneV: gphoneV
    })
  },

  getInfo: function() {
    let that = this;
    let data = {

    }
  },
  //获取手机号码
  gphone: function() {
    let that = this;
    if (that.data.gphoneV == '') {
      Function.layer('请输入手机号码');
      return;
    } else if (!app.validatePhone(that.data.gphoneV)) {
      Function.layer('手机号码有误');
      return;
    }
    let data = {
      uid: wx.getStorageSync('members').id,
      phone: that.data.gphoneV

    }
    index.jian(data, (res) => {
      if (res.code == 200) {
        Function.layer('你已领取' + res.data + "红包");
        that.setData({
          zfad: false
        })
      } else {
        Function.layer(res.msg);
        if (res.msg == '请先绑定手机') {
          wx.showModal({
            title: '提示',
            content: '你还未绑定手机是否前往绑定',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/shouye/bindMobile/bindMobile'
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })

        }
      }
    })
  },

  _getrcoeB(type) { 
    let that = this;
    let data={
      type: type
    };
    index.getrcoeB(data, (res) => {
      if (res.code == 200) {
        that.setData({
          wtype: res.data
        })
      }
    })
  },
  _getrcoeC(type, oid, title) {
    let that = this;
    let data = {
      type: type,
      id: oid
    };
    if (type==1){
      data.pain = title
    }
    if (type == 2) {
      data.mode = title
    }
    if (type == 3) {
      data.time = title
    }
    if (type == 4) {
      data.past = title
    }
    index.getrcoeB(data, (res) => {
      if (res.code == 200) {
        that.setData({
          wtype: res.data
        })
      }
    })
  },


  closemode(){
    this.setData({
      showpain:false
    })
  },
  selpain(e){
    let type = e.currentTarget.dataset['type'];
    // let oid = e.currentTarget.dataset['index'];
    let oid = wx.getStorageSync('members').id;
    let title = e.currentTarget.dataset['title'];
    this.setData({
      showpain: false
    })
    this._getrcoeC(type, oid, title)
    let data = {
      id: wx.getStorageSync('members')['id']
    };
    let that =this
    index.getrcoe(data, (res) => {
      if (res.code == 200) {
        that.setData({
          tq: res.data
        })
      }
    })
  },
 

  onReady: function() {

  },

  onShow: function() {

  },

  onHide: function() {

  },

  onUnload: function() {

  },

  onPullDownRefresh: function() {

  },

  onReachBottom: function() {

  },

  onShareAppMessage: function() {

  }
})